package com.bnyy.medicalHousekeeper.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.bnyy.medicalHousekeeper.request.RequestManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

public abstract class BaseFragment extends Fragment {

    public AppCompatActivity mContext;
    public LayoutInflater inflater;
    public RequestManager requestManager;
    public EventBus mEventBus;

    public abstract int getLayoutId();

//    public abstract Fragment getSelfFragment();

    public abstract boolean registerEventBus();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = (AppCompatActivity) getActivity();
        inflater = LayoutInflater.from(getActivity());
        requestManager = RequestManager.getInstance();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(getLayoutId(), container, false);
        ButterKnife.bind(this, contentView);

        return contentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (registerEventBus()){
            mEventBus = EventBus.getDefault();
            mEventBus.register(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (registerEventBus()) {
            mEventBus.unregister(this);
        }
    }
}
